﻿using BlogData.Models;
using FreeSql;


namespace BlogWeb.Services
{
    public class PostService
    {
        private  IBaseRepository<Post> _post;
        private readonly IFreeSql _freeSql;
        public PostService(IBaseRepository<Post> post,  IFreeSql freeSql) 
        {
            _post = post;
            _freeSql = freeSql;
        }

        /// <summary>
        /// 获取所有文章
        /// </summary>
        /// <returns></returns>
        public List<Post>  GetAllList()
        {
            return _post.Select.ToList();
        }



        /// <summary>
        /// 获取分类所有文章
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<Post> GetPostByCategory(string category)
        {
            var data = _freeSql.Select<Post>()
            .Where(x => x.PageCategory == category)
            .ToList();
            return data;
        }

        /// <summary>
        /// 保存或者更新，返回影响行数
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public bool SaveOrUpdate(Post post)
        {
            bool res = false;
            int s = 0;
            var isPost =  _post.Select.Where(x => x.PageTitle == post.PageTitle).First();
            if(isPost == null)
            {
                s =_freeSql.Insert(post).ExecuteAffrows();
            }
            else
            {
                 s = _post.Update(post); 
            }
            return res;
        }

        //文章查询，模糊查询
        public List<Post> GetPostById(string title)
        {
            return _post.Select.Where(p => p.Contains('title')).ToList();
           
        }



        //根据文章状态查询文章，发表状态、完成状态
        public List<Post> GetPostStatus(string status, int ispublish = 0)
        {
            return _post.Select.Where(p => p.IsPublish == ispublish && p.Status == status).ToList();
        }

        //  时间查询
    }
}
